package routes

import (
	"fmt"
	"go.uber.org/zap"
	"net/http"
	"web_app/controllers"
	"web_app/logger"
	"web_app/tools/middleware"

	"github.com/gin-gonic/gin"
	"github.com/shirou/gopsutil/host"
)

var routeNum int

func RouteInit() *gin.Engine {
	r := gin.New()
	r.Use(logger.GinLogger(), logger.GinRecovery(true))
	//r := gin.Default()
	nInfo, _ := host.Info()

	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusOK, "not find route")
	})

	r.GET("/", func(c *gin.Context) {
		routeNum++
		zap.L().Info("请求根目录:", zap.String("Hostname:", nInfo.Hostname))
		zap.L().Error("请求根目录:", zap.String("remoteIp:", c.RemoteIP()))
		zap.L().Debug("请求根目录:", zap.String("remoteIp:", c.RemoteIP()))
		c.String(http.StatusOK, "hello world ！"+fmt.Sprintf("%d", routeNum)+": hostname:"+nInfo.Hostname+"\n")
	})

	v1 := r.Group("/api/v1")

	v1.POST("/post", func(c *gin.Context) {
		c.String(http.StatusOK, "hello world")
	})

	//校验jwt
	v1.POST("/auth", middleware.JWTAuthMiddleware(), func(c *gin.Context) {
		fmt.Println(c.GetString("username"))
		fmt.Println(c.GetInt64("user_id"))
		c.String(http.StatusOK, "我已经进行了登录")
	})
	//注册业务
	{
		v1.POST("/signup", controllers.SignUpHandler)
	}

	//v1.Use(middleware.JWTAuthMiddleware())

	{
		v1.POST("/users", controllers.UserListHandler)

		v1.GET("/user/tag", controllers.UserTagHandler)
	}

	return r
}
